home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 641 / text0000.txt < prev   
Encoding:
Text File  |  1996-08-06  |  3.8 KB  |  163 lines

  1. I have 4 ideas about C++ and a question.
  2.  
  3.  
  4. 1) String literals
  5. ~~~~~~~~~~~~~~~~~~
  6. String literals should be const char* instead of char* (if it is not
  7. already the case).
  8.  
  9.  
  10. 2) Placement operators
  11. ~~~~~~~~~~~~~~~~~~~~~~
  12. class Object {
  13.         void    operator+ // or / or whatever
  14.                          (Object& result, const Object& b) const;
  15.         void    operator* (Object& result) const;
  16.         Object  operator* (const Object& result) const; // old one
  17. }
  18.  
  19. should be called respectively by:
  20. a = b + c;
  21.  
  22. and a = *b;
  23.  
  24. and (old one) a*b;
  25.  
  26. This will solve the [well-known] problem of copying the result into a temporary.
  27. The compatibility will be preserved since old syntax will be allowed to.
  28.  
  29. 3) Use of explicit keyword
  30. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  31. //========= Test.h =========
  32. class Base {
  33.     public:
  34.         virtual void VirtualFunc ();
  35. };
  36.  
  37. class Derived : public Base {
  38.     public:
  39.         void VirtualFunc () { }
  40. };
  41.  
  42. //========= Caller.cp =========
  43. #include <Test.h>
  44. class Derived2 : public Derived {
  45.     public:
  46.         void VirtualFunc ();
  47. };
  48.  
  49. //========= Foo.cp =========
  50. #include <Test.h>
  51.  
  52. void    foo (explicit Derived* object)
  53. {
  54.     object.VirtualFunc (); // nop (compiler does not generate code for this)
  55.  
  56.     Derived* AnyDerivedObject = object; // ok
  57. }
  58.  
  59. void    foo2 (Derived* object)
  60. {
  61.     object.VirtualFunc (); // object can be Derived2
  62.  
  63.     explicit Derived* RealDerivedObject = object; // error
  64. }
  65.  
  66. The meaning of explicit could be: while a derived class ptr [resp. ref] can be
  67. convert to a base class ptr [ref], it can't be converted to a base class 
  68. explicit ptr [ref].
  69.  
  70. So explicit would be a type qualifier; this usage is different with:
  71.  
  72. > Rich Hickey
  73. > rich@rcs-hq.mhs.compuserve.com
  74.  
  75. > I propose that the explicit keyword be allowed as a qualifier of a class
  76. > definition, such qualification taking the form of:
  77.  
  78. > explicit class X{
  79. >   //the definition of X
  80. >   };
  81.  
  82. Here, X cannot be derived but my Derived can be so I think my idea is better
  83. (please fell free comment/criticise this assertion).
  84.  
  85.  
  86. 4) Conversion of Type** to const Type**
  87. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88.  
  89. It's ok, this is an error; but why cannot Type** be converted to const Type* 
  90. const* ?
  91.  
  92. void    foo ()
  93. {
  94.     typedef int Type;
  95.     Type        i;
  96.     const Type  ci = 2;
  97.     Type*       pi = &i;
  98.     Type**      ppi = π
  99.  
  100.     const Type** ppci = ppi;            // error because
  101.     *ppci = &ci;                        // now pi == &ci
  102.     *pi = 0;                            // and now we modify ci
  103.  
  104.     const Type*const* pcpci = ppi;      // no problem because
  105.     *pcpci = &ci;                       // this is impossible (since *pcpci is 
  106. const)
  107. }
  108.  
  109.  
  110. Now a question:
  111.  
  112. Will there be any way to redefine (overload) normal && or || on class ?
  113. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  114.  
  115. I mean, now:
  116.  
  117. class Int {
  118.     public:
  119.         operator int () const { return i; }
  120.         operator && (int) const;
  121.         operator || (int) const;
  122.         Int (int I) : i(I) { }
  123.  
  124.     private:
  125.         int     i;
  126. };
  127.  
  128. int     Func ()
  129. {
  130.     cout << "called\n";
  131.     return 1;
  132. }
  133.  
  134. void    foo ()
  135. {
  136.     int i (1);
  137.     Int I (1);
  138.  
  139.     if (i || Func ()) // called is not printed
  140.         ;
  141.     if (I || Func ()) // called is printed
  142.         ;
  143. }
  144.  
  145. One could think that the standard should be change in order to support real
  146. overloaded operator && and ||.
  147.  
  148. I can't see the point of doing that, so if it's the case for you to don't ask
  149. me nor criticise.
  150.  
  151. Valentin Bonnard
  152. bonnardv@pratique.fr
  153.  
  154.  
  155.  
  156.  
  157. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your
  158.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  159.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  160.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  161.   Comments? mailto:std-c++-request@ncar.ucar.edu
  162. ]
  163.